package my.yuan.fkzapp.response;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import my.yuan.fkzcommon.domain.code.ICode;
import my.yuan.fkzcommon.domain.code.ResponseCode;

import java.io.Serializable;

/**
 * @author qy
 * @description Result
 * @date 2021-10-01
 */
@ApiModel(value = "全局返回结果")
@Data
public class CommonResult<T> implements Serializable {

    private static final long serialVersionUID = -5458429370303545954L;
    @ApiModelProperty(value = "返回码")
    private Integer code;
    @ApiModelProperty(value = "返回消息")
    private String message;
    @ApiModelProperty(value = "返回数据")
    private T data;

    public CommonResult() {
    }

    public CommonResult(Integer code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public static <T> CommonResult<T> success(T data) {
        return new CommonResult<>(ResponseCode.SUCCESS.getCode(), ResponseCode.SUCCESS.getMessage(), data);
    }

    public static <T> CommonResult<T> fail(String msg) {
        return new CommonResult<>(ResponseCode.FAILED.getCode(), msg, null);
    }

    public static <T> CommonResult<T> fail(ICode code, String msg) {
        return new CommonResult<>(code.getCode(), msg, null);
    }

    public static <T> CommonResult<T> fail(ICode code) {
        return new CommonResult<>(code.getCode(), code.getMessage(), null);
    }
}
